<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="活动名称" prop="title">
            <el-input v-model.trim="form.title"></el-input>
          </el-form-item>
          <el-form-item label="业务范围" prop="courseType">
            <ExtFiledSelect
              v-if="dialogVisible"
              :defaultValue="form.ruleVO.courseType"
              @emit="setExtFiled"
              :disabled="title != '新增'"
              :scoped="[0, 6]"
            />
          </el-form-item>
          <el-form-item label="承办单位" prop="addr">
            <el-input v-model.trim="form.addr"></el-input>
          </el-form-item>
          <!-- <el-form-item label="内容">
            <el-input v-model.trim="form.content"></el-input>
          </el-form-item> -->
          <el-form-item label="起止时间" prop="date">
            <el-date-picker
              v-model="form.date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="描述">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入描述"
              v-model="form.des"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  expertsReviewAdd,
  expertsReviewUpdate,
} from '@/api/admin/audit/expertsReview.js'
import Bus from '@/utils/bus.js'
export default {
  components: {
    ExtFiledSelect: () => import('@/components/dataSelect/extFiledSelect'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        courseType: '',
        title: '', //活动名称
        date: [],
        addr: '', //地址
        content: '', //内容
        extFiled: '',
        startTime: '',
        endTime: '',
        status: '1', //状态，1-待评审，2-评审中，3-评审完成
        des: '', //描述
        ruleVO: {
          courseType: '',
          deptId: '',
          rule1: '',
          rule2: '',
        },
      },
      rules: {
        title: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        addr: [{ required: true, message: '请输入承办单位', trigger: 'blur' }],
        courseType: [
          { required: true, message: '请选择业务范围', trigger: 'blur' },
        ],
        date: [{ required: true, message: '请选择起止时间', trigger: 'blur' }],
      },
    }
  },
  props: ['scope'],
  mounted() {
    Bus.$on('showDialogReviewAdd', this.init)
  },
  beforeDestroy() {
    Bus.$off('showDialogReviewAdd')
  },
  methods: {
    setExtFiled(v) {
      this.form.courseType = v
      this.form.ruleVO.courseType = v
    },
    //显示弹窗
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.form.date = [this.form.startTime, this.form.endTime]
        this.form.ruleVO = JSON.parse(row.extFiled)
        this.form.courseType = this.form.ruleVO.courseType
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.startTime = this.form.date[0]
      this.form.endTime = this.form.date[1]
      // this.form.date = undefined
      this.form.ruleVO.deptId = this.scope
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.title == '新增' ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      expertsReviewAdd(this.form).then((res) => {
        this.done(res)
      })
    },
    //编辑
    edit() {
      expertsReviewUpdate(this.form).then((res) => {
        this.done(res)
      })
    },
    done(res) {
      if (!res || res.data.code != 0) return
      this.dialogVisible = false
      Bus.$emit('refreshReviewList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
    },
  },
}
</script>
